package problem;

import java.util.Arrays;

public class S_135 {
    //分发糖果，我的暴力有错。。。
    class Solution {
        public int candy(int[] ratings) {
            int n=ratings.length;
            int []a=new int[n];
            for(int i=0;i<n;i++) a[i]=1;    //每人初始化一个糖果,Arrays.fill(a,1);
            boolean flag=true;              //记录每次糖果数组是否有变化
            int sum=0;
            while(flag){
                flag=false;
                for(int i=0;i<n;i++){
                    //遍历每个孩子，让其先和前面的孩子比较，再和后面的孩子比较
                    if(i>0&&ratings[i-1]<ratings[i]){
                        if(a[i-1]<a[i]) continue;
                        else{
                            a[i]+=1;
                            flag=true;
                        }
                    }
                    if(i<n-1&&ratings[i]>ratings[i+1]){
                        if(a[i]>a[i+1]) continue;
                        else{
                            a[i]+=1;
                            flag=true;
                        }
                    }
                }
            }
            for(int candy:a) sum+=candy;
            return sum;
        }
    }
    //官方的暴力...
    public class Solution1 {
        public int candy(int[] ratings) {
            int[] candies = new int[ratings.length];
            Arrays.fill(candies, 1);
            boolean flag = true;
            int sum = 0;
            while (flag) {
                flag = false;
                for (int i = 0; i < ratings.length; i++) {
                    if (i != ratings.length - 1 && ratings[i] > ratings[i + 1] && candies[i] <= candies[i + 1]) {
                        candies[i] = candies[i + 1] + 1;
                        flag = true;
                    }
                    if (i > 0 && ratings[i] > ratings[i - 1] && candies[i] <= candies[i - 1]) {
                        candies[i] = candies[i - 1] + 1;
                        flag = true;
                    }
                }
            }
            for (int candy : candies) {
                sum += candy;
            }
            return sum;
        }
    }

}
